import { app, BrowserWindow,Menu,ipcMain ,screen,Tray } from 'electron'
// import winURL from './evnconfig'
var screenshot = require('desktop-screenshot');

const path = require('path');
import '../renderer/store'
/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow
let childWindow
let tray = null
const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`

  
function createWindow () {
  /**
   * Initial window options
   */
  //设置底部标签栏
  tray = new Tray(`${__static}/icon.ico`)
  // console.log(require('path').join(__dirname, '/imgs/icon.ico').replace(/\\/g, '\\\\'))
  const contextMenu = Menu.buildFromTemplate([
    { label: '退出', click:function(){app.quit()} },
  ])
  tray.setToolTip('气泡提示')
  tray.on('click', (event, bounds) => {
    if (mainWindow.isVisible()) {
      mainWindow.hide()
    } else {
      mainWindow.show()
    }
  })
  tray.setContextMenu(contextMenu)

  //去掉头部便签兰
  Menu.setApplicationMenu(null)
  const { width, height } = screen.getPrimaryDisplay().workAreaSize;
 
  let x = width-380;
  let y= height-630;
  mainWindow = new BrowserWindow({
    width: 300,
    height: 620,
    minWidth: 300,
    minHeight: 620,
    x:x,
    y:y,
    useContentSize: true,
    icon: 'build/icons/icon.ico',
    frame: false,
    resizable: false,
    title:"聊天",
    webPreferences: {
      nodeIntegration: true,
      nodeIntegrationInWorker: true,
      webSecurity: false,
      navigateOnDragDrop: true,
      devTools: true
    }

  })

  mainWindow.loadURL(winURL)

  mainWindow.on('closed', () => {
    mainWindow = null
  })
}

app.on('ready', createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})
//接收最小化命令
// windowMin(mainWindow)
ipcMain.on('window-min', function() {
  mainWindow.minimize();
})
//接收关闭命令
ipcMain.on('window-close', function() {
  mainWindow.close();
  mainWindow = null
})
//接收显示命令
ipcMain.on('window-show', function() {
  mainWindow.show();
})

//接收隐藏命令
ipcMain.on('window-hide', function() {
  mainWindow.hide();
})

//接收截图命令
ipcMain.on('window-screenShot', function() {
  var imgurl = require('path').join(__static, 'screenshot.png').replace(/\\/g, '\\\\')
  screenshot(imgurl, function(error, complete) { 
    if(error) {
      console.log("Screenshot failed", error); 
    }else{
      console.log("Screenshot succeeded")
      console.log(complete)
    } 
  }); 
    // const { execFile } = require('child_process')
   
    // var screenWin = execFile(`${__static}/PrScrn.exe`)
    // var screenWin = execFile(require('path').join(__static, '/PrintScreen - CHIP-Installer.exe').replace(/\\/g, '\\\\'))

		// screenWin.on('exit', function(code) {
		// 	let pngs = require('electron').clipboard.readImage().toPNG()
		// 	let imgData = new Buffer.from(pngs, 'base64')
		// 	let imgs = 'data:image/png;base64,' + btoa(new Uint8Array(imgData).reduce((data, byte) => data + String.fromCharCode(byte), ''))
		// 	// console.log(imgs)
  // })

  
})
//创建子窗口
ipcMain.on('creat-child-window', function(event,data) {
  if(data.type=='min'){
    childWindow = new BrowserWindow({
      width: 320,
      height: 420,
      center:true,
      resizable: false,
      parent:mainWindow
    })
  }else {
    childWindow = new BrowserWindow({
      title:'聊天',
      width: 800,
      height: 620,
      // frame: false,
      resizable: false,
      center:true,
      parent:mainWindow
    })
  }
  var childurl = winURL + data.url
  childWindow.loadURL(childurl)
  childWindow.on('closed',function(){
    console.log("子窗口关闭")
    childWindow = null
    
  })
})

// child-window-close
ipcMain.on('child-window-close', function(){
  childWindow.close()
})

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
